package com.seafood.controller;

import com.seafood.common.PageResult;
import com.seafood.common.Result;
import com.seafood.dto.*;
import com.seafood.service.AdminUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * 管理员用户管理控制器
 */
@Tag(name = "管理员用户管理", description = "管理员用户管理相关接口")
@RestController
@RequestMapping("/admin/users")
@RequiredArgsConstructor
@PreAuthorize("hasRole('ADMIN')")
public class AdminUserController {

    private final AdminUserService adminUserService;

    /**
     * 分页查询用户列表 - GET方式
     */
    @Operation(summary = "分页查询用户列表", description = "支持按用户类型、状态、关键词等条件筛选")
    @GetMapping
    public Result<PageResult<UserDTO>> getUserList(
            @Parameter(description = "页码", example = "1")
            @RequestParam(defaultValue = "1") Integer page,
            
            @Parameter(description = "每页大小", example = "10") 
            @RequestParam(defaultValue = "10") Integer size,
            
            @Parameter(description = "用户类型")
            @RequestParam(required = false) String userType,
            
            @Parameter(description = "用户状态")
            @RequestParam(required = false) String status,
            
            @Parameter(description = "关键词搜索(用户名/邮箱/手机号)")
            @RequestParam(required = false) String keyword) {
        
        UserQueryDTO queryDTO = new UserQueryDTO();
        queryDTO.setPage(page);
        queryDTO.setSize(size);
        queryDTO.setUserType(userType);
        queryDTO.setStatus(status);
        queryDTO.setKeyword(keyword);
        
        PageResult<UserDTO> result = adminUserService.getUserList(queryDTO);
        return Result.success("查询成功", result);
    }

    /**
     * 分页查询用户列表 - POST方式(支持复杂查询条件)
     */
    @Operation(summary = "分页查询用户列表", description = "POST方式支持复杂查询条件")
    @PostMapping("/query")
    public Result<PageResult<UserDTO>> getUserListByPost(
            @Valid @RequestBody UserQueryDTO queryDTO) {
        PageResult<UserDTO> result = adminUserService.getUserList(queryDTO);
        return Result.success("查询成功", result);
    }

    /**
     * 获取用户详情
     */
    @Operation(summary = "获取用户详情", description = "根据用户ID获取详细信息")
    @GetMapping("/{id}")
    public Result<UserDTO> getUserDetail(
            @Parameter(description = "用户ID", required = true)
            @PathVariable Long id) {
        UserDTO userDTO = adminUserService.getUserDetail(id);
        return Result.success("查询成功", userDTO);
    }

    /**
     * 创建用户
     */
    @Operation(summary = "创建用户", description = "管理员创建新用户")
    @PostMapping
    public Result<UserDTO> createUser(
            @Valid @RequestBody UserCreateDTO createDTO) {
        UserDTO userDTO = adminUserService.createUser(createDTO);
        return Result.success("用户创建成功", userDTO);
    }

    /**
     * 更新用户信息
     */
    @Operation(summary = "更新用户信息", description = "管理员更新用户信息")
    @PutMapping("/{id}")
    public Result<UserDTO> updateUser(
            @Parameter(description = "用户ID", required = true)
            @PathVariable Long id,
            @Valid @RequestBody UserUpdateDTO updateDTO) {
        UserDTO userDTO = adminUserService.updateUser(id, updateDTO);
        return Result.success("用户更新成功", userDTO);
    }

    /**
     * 更新用户状态
     */
    @Operation(summary = "更新用户状态", description = "启用或禁用用户账户")
    @PutMapping("/{id}/status")
    public Result<Void> updateUserStatus(
            @Parameter(description = "用户ID", required = true)
            @PathVariable Long id,
            @Valid @RequestBody UserStatusUpdateDTO statusUpdateDTO) {
        adminUserService.updateUserStatus(id, statusUpdateDTO);
        return Result.success("状态更新成功", null);
    }

    /**
     * 重置用户密码
     */
    @Operation(summary = "重置用户密码", description = "管理员重置用户密码为默认密码")
    @PutMapping("/{id}/reset-password")
    public Result<Map<String, String>> resetUserPassword(
            @Parameter(description = "用户ID", required = true)
            @PathVariable Long id) {
        String newPassword = adminUserService.resetUserPassword(id);
        Map<String, String> result = new HashMap<>();
        result.put("newPassword", newPassword);
        return Result.success("密码重置成功", result);
    }

    /**
     * 删除用户
     */
    @Operation(summary = "删除用户", description = "软删除用户账户")
    @DeleteMapping("/{id}")
    public Result<Void> deleteUser(
            @Parameter(description = "用户ID", required = true)
            @PathVariable Long id) {
        adminUserService.deleteUser(id);
        return Result.success("用户删除成功", null);
    }

    /**
     * 获取用户统计信息
     */
    @Operation(summary = "获取用户统计信息", description = "获取用户注册趋势和分布统计")
    @GetMapping("/statistics")
    public Result<?> getUserStatistics() {
        var statistics = adminUserService.getUserStatistics();
        return Result.success("查询成功", statistics);
    }

    /**
     * 批量更新用户状态
     */
    @Operation(summary = "批量更新用户状态", description = "批量启用或禁用多个用户")
    @PutMapping("/batch/status")
    public Result<Void> batchUpdateUserStatus(
            @RequestBody @Valid UserStatusUpdateDTO statusUpdateDTO) {
        adminUserService.batchUpdateUserStatus(statusUpdateDTO);
        return Result.success("批量更新成功", null);
    }

    /**
     * 导出用户数据
     */
    @Operation(summary = "导出用户数据", description = "导出Excel格式的用户数据")
    @PostMapping("/export")
    public Result<String> exportUsers(
            @RequestBody UserQueryDTO queryDTO) {
        // TODO: 实现Excel导出功能
        return Result.success("导出功能尚未实现", "尚未实现");
    }
}